// pages/test-mode/test-mode.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    num: 0,
    timer: 30,
    textTime: 0,
    bfb: 0,
    resultArr: {},
    result: [],
    show: false,
    submitShow: false,
    shows: false,
    mins: 0,
    secode: 0,
    fen: 0
  },
  submitFn() {
    if (this.data.num == this.data.list.length - 1) {
      this.setData({
        submitShow: true
      });
    }
  },

  btnFn(val) {
    this.setData({
      num: val.currentTarget.dataset.index
    })
    if (this.data.resultArr[this.data.num]) {
      this.setData({
        result: this.data.resultArr[this.data.num],
      })
    } else {
      this.setData({
        result: []
      })
    }
    if (this.data.resultArr[this.data.num]) {
      this.setData({
        result: this.data.resultArr[this.data.num]
      })
    }
  },

  btnclick() {
    this.setData({
      show: true
    })
  },

  onClose() {
    this.setData({
      show: false,
      submitShow: false,
      dialogShow: false
    });
  },

  timers() {
    let oldTime = this.data.timer * 60
    let newtimers = oldTime;
    let newtimer = setInterval(() => {
      let hours = Math.floor(newtimers / 3600);
      let min = Math.floor(newtimers / 60);
      let s = newtimers % 60;
      if (newtimers == 0) {
        clearInterval(newtimer)
      } else {
        newtimers--;
      }
      this.setData({
        timer: newtimers,
        textTime: this.fill0(hours) + ":" + this.fill0(min) + ":" + this.fill0(s),
        bfb: Math.floor(newtimers / oldTime * 100)
      })
      console.log(oldTime / 60)
      if (this.data.submitShow) {
        clearInterval(newtimer)
        this.setData({
          mins: ((oldTime / 60) - 1) - min,
          secode: 60 - s
        })
      }
    }, 1000)

  },

  fill0(val) {
    return val < 10 ? '0' + val : val
  },

  onChange(event) {
    this.setData({
      result: event.detail,
    })
  },

  getList() {
    wx.request({
      url: 'http://172.20.10.3:3001/questions/randList',
      data: {
        pageSize: 1,
        currentPage: 1
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        res.data.data.forEach(value => {
          value.options = JSON.parse(value.options)
          value.answers = JSON.parse(value.answers)
          value.type = 'primary'
        })
        this.setData({
          list: res.data.data
        })
      },
    })
  },

  // 下一题
  nextQuestion() {
    this.setData({
      resultArr: {
        ...this.data.resultArr,
        [this.data.num]: this.data.result.sort()
      },
      result: []
    })
    if (this.data.num < this.data.list.length - 1) {
      this.setData({
        num: this.data.num + 1,
      });
    }
    if (this.data.resultArr[this.data.num]) {
      this.setData({
        result: this.data.resultArr[this.data.num]
      })
    }
    let data = this.data.list
    if (JSON.stringify(this.data.resultArr[this.data.num - 1]) == JSON.stringify(this.data.list[this.data.num - 1].answers)) {
      this.data.list[this.data.num - 1].type = 'success'
      this.setData({
        list: data,
        fen: this.data.fen + 5
      })
    } else {
      if (this.data.resultArr[this.data.num - 1].length) {
        this.data.list[this.data.num - 1].type = 'danger'
        this.setData({
          list: data
        })
      } else {
        return
      }
    }
  },

  // 上一题
  lastQuestion() {

    if (this.data.num > 0) {
      this.setData({
        num: this.data.num - 1,
      });
    }
    if (this.data.resultArr[this.data.num]) {
      this.setData({
        result: this.data.resultArr[this.data.num]
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
    this.timers()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})